package cn.panda.edu.controller;

import cn.panda.edu.model.ThesisLayout;
import cn.panda.edu.service.ThesisLayoutService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.FileUtil;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 类描述：教师论文发布管理API  
 * @author: DuanGaoXi
 * @date： 2018-01-18 上午9:46:51
 */
@RestController
@RequestMapping(value="/api/admin")
@Api(description="教师论文发布管理")
public class ManageThesisLayoutController {
	@Autowired
	private ThesisLayoutService thesisLayoutService;
	
	/**
	 * 
	* @Title: insertThesisLayout 
	* @Description: TODO(上传论文规范) 
	* @param @param file
	* @param @param request
	* @param @return  
	* @return Object
	 */
	@ApiOperation(value="上传论文规范")
	@RequestMapping(value="/uploadThesisLayout",method=RequestMethod.POST)
	public  Object uploadThesisLayout(Integer thesisLayoutId ,@RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
		
        String filePath = null ;
        try {
            filePath = FileUtil.uploadFile(file) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        ThesisLayout thesisLayout = new ThesisLayout();
        thesisLayout.setThesisLayoutId(thesisLayoutId);
        thesisLayout.setThesisLayoutName(file.getOriginalFilename());
        System.out.println(file.getOriginalFilename());
        thesisLayout.setThesisLayoutContent(filePath);
        ThesisLayoutmanageUp(thesisLayout);
		return ResultDataUtil.resData(200, "上传成功！", filePath);
	}
	
	
	/**
	 * 
	* @Title: papermanageFy 
	* @Description: 分页模糊查询（论文规范ID+论文规范名）
	* @param @param thesisLayoutId
	* @param @param thesisLayoutName
	* @param @param pageIndex
	* @param @return  
	* @return PageInfo<ThesisLayout>
	 */
	@ApiOperation(value="模糊查询论文",notes="模糊查询+分页")
	@RequestMapping(value = "/ThesisLayoutmanageFy", method = RequestMethod.POST)
	@ApiImplicitParams({
						@ApiImplicitParam(name = "thesisLayoutId", value = "论文规范ID", required = false, dataType = "int",paramType="form"),
						@ApiImplicitParam(name = "thesisLayoutName", value = "论文规范名", required = false, dataType = "string",paramType="form"),
						@ApiImplicitParam(name = "pageIndex", value = "分页页数", required = true, dataType = "int",paramType="form")
})
	public Object ThesisLayoutmanageFy(Integer thesisLayoutId,String thesisLayoutName,Integer pageIndex){
		System.out.println(thesisLayoutName);
		PageInfo<ThesisLayout> pThesisLayout = thesisLayoutService.selectLikeByPageSelective(thesisLayoutId, thesisLayoutName, pageIndex,Constant.PAGE_SIZE);
		
		return ResultDataUtil.resData(200, "", pThesisLayout);
	}
	/**
	 * 
	* @Title: papermanageDel 
	* @Description: 主键删除论文规范
	* @param @param thesisLayoutId
	* @param @return  
	* @return Map<String,Object>
	 */
	@ApiOperation(value="根据主键删除论文规范",notes="主键删除")
	@RequestMapping(value = "/ThesisLayoutmanageDel", method = RequestMethod.POST)
	@ApiImplicitParam(name = "thesisLayoutId", value = "论文规范ID", required = true, dataType = "int",paramType="form")
	public Map<String, Object> ThesisLayoutmanageDel(Integer thesisLayoutId) {
		Map<String, Object> map = new HashMap<String, Object>();
		int result = thesisLayoutService.deleteByPrimaryKey(thesisLayoutId);
		if (result > 0) {
			map = ResultDataUtil.resData(200, "删除成功",null) ;
		} else {
			map = ResultDataUtil.resData(500, "删除失败",null) ;
		}
		return map;
	}
	
	/**
	 * 
	* @Title: papermanageAdd 
	* @Description: 动态添加论文规范信息
	* @param @param record
	* @param @return  
	* @return Map<String,Object>
	 */
	@ApiOperation(value="添加论文规范",notes="动态添加")
	@RequestMapping(value="/ThesisLayoutmanageAdd", method = RequestMethod.POST)
	@ApiImplicitParam(name = "ThesisLayout", value = "论文对象", dataType = "object",paramType="form")
	public  Map<String, Object> ThesisLayoutmanageAdd(ThesisLayout record) {
		Map<String, Object> map = new HashMap<String, Object>();
		int result = thesisLayoutService.insert(record);
		if (result > 0) {
			map = ResultDataUtil.resData(200, "添加成功",null) ;
		} else {
			map = ResultDataUtil.resData(200, "添加失败",null) ;
		}
		return map;
	}

	
	/**
	 * 
	* @Title: papermanageDel 
	* @Description: 根据主键更新论文
	* @param @param record
	* @param @return  
	* @return Map<String,Object>
	 */
	@ApiOperation(value="根据主键更新论文规范",notes="有选择更新")
	@RequestMapping(value = "/ThesisLayoutmanageUp", method = RequestMethod.POST)
	@ApiImplicitParam(name = "ThesisLayout", value = "论文规范对象", required = false, dataType = "object",paramType="form")
	public Map<String, Object> ThesisLayoutmanageUp(ThesisLayout record) {
		Map<String, Object> map = new HashMap<String, Object>();
		record.setThesisLayoutUpdate(new Date());
		int result =  thesisLayoutService.updateByPrimaryKeySelective(record);
		if (result > 0) {
			map = ResultDataUtil.resData(200, "更新成功",null) ;
		} else {
			map = ResultDataUtil.resData(500, "更新失败",null) ;
		}
		return map;
	}
	
	
	/**
	 * 
	* @Title: papermanageSel 
	* @Description: 根据主键查询规范
	* @param @param thesisLayoutId
	* @param @return  
	* @return Map<String,Object>
	 */
	@ApiOperation(value="id查询论文规范",notes="根据主键查询")
	@RequestMapping(value = "/ThesisLayoutmanageSel", method = RequestMethod.POST)
	@ApiImplicitParam(name = "thesisLayoutId", value = "论文规范ID", required = true, dataType = "int",paramType="form")
	public Map<String, Object> ThesisLayoutmanageSel(@RequestParam(required=true)Integer thesisLayoutId) {
		Map<String, Object> map = new HashMap<String, Object>();
		ThesisLayout thesisLayout = thesisLayoutService.selectByPrimaryKey(thesisLayoutId);
		return ResultDataUtil.resData(200, "", thesisLayout);
	}
}
